package com.ysstech.reportworld.controller;


import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysstech.common.annotation.YssLog;
import com.ysstech.common.annotation.YssPermissions;
import com.ysstech.common.controller.BaseController;
import com.ysstech.common.exception.BusinessException;
import com.ysstech.common.util.DateUtil;
import com.ysstech.common.util.resultutil.RestResult;
import com.ysstech.common.util.resultutil.ResultGenerator;
import com.ysstech.reportworld.entity.TableCheck;
import com.ysstech.reportworld.enums.ReportWorldEnum;
import com.ysstech.reportworld.service.TableCheckService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 标准层表校验语句信息 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-05-19
 */
@Controller
@Slf4j
@Api(tags = "报表世界标准层表校验语句句管理api")
@RequestMapping("/tableCheck")
public class TableCheckController extends BaseController {

    @Autowired
    private TableCheckService tableCheckService;

    /**
     * 标准层表校验语句列表查询
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table"})
    @ApiOperation(value = "获取所有标准层表校验语句信息")
    @YssLog(module = "标准层表校验语句管理", remark = "获取所有标准层表校验语句信息")
    @PostMapping(value = "/listInfo")
    @ResponseBody
    public RestResult listInfo(TableCheck tableCheck) throws Exception {
        if (null == tableCheck || StringUtils.isBlank(tableCheck.getTableId())) {
            throw new BusinessException("获取新增报表世界标准层表校验语句信息失败，请稍后重试！");
        }
        QueryWrapper<TableCheck> wrapper = new QueryWrapper<>();
        wrapper.eq("TABLE_ID", tableCheck.getTableId());
        if (StringUtils.isNotBlank(tableCheck.getErrorMsg())) {
            wrapper.like("ERROR_MSG", tableCheck.getErrorMsg());
        }
        if (StringUtils.isNotBlank(tableCheck.getVersion())) {
            wrapper.eq("VERSION", tableCheck.getVersion());
        }
        if (null != tableCheck.getErrorType()) {
            wrapper.eq("ERROR_TYPE", tableCheck.getErrorType());
        }
        wrapper.orderByDesc("STATUS");
        wrapper.orderByAsc("SEQUENCE");
        List<TableCheck> list = tableCheckService.list(wrapper);
        return ResultGenerator.getSuccessResult("获取所有SQL信息成功", list);
    }

    /**
     * 新增标准层表校验语句的方法
     *
     * @param tableCheck
     * @return
     */
    @YssPermissions(value = {"reportworld:table:check:add"})
    @ApiOperation(value = "新增标准层表校验语句信息")
    @YssLog(module = "标准层表校验语句管理", remark = "新增标准层表校验语句信息")
    @RequestMapping(value = "/add")
    @ResponseBody
    public RestResult add(TableCheck tableCheck) throws Exception {
        if (null == tableCheck || StringUtils.isBlank(tableCheck.getTableId())) {
            throw new BusinessException("获取新增报表世界标准层表校验语句信息失败，请稍后重试！");
        }
        tableCheck.setId(DateUtil.generateUUID());
        tableCheck.setAddUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableCheck.setAddTime(LocalDateTime.now());
        tableCheckService.save(tableCheck);
        return ResultGenerator.getSuccessResult("新增标准层表校验语句信息成功！");
    }

    /**
     * 修改标准层表校验语句信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:check:edit"})
    @ApiOperation(value = "修改标准层表校验语句信息")
    @YssLog(module = "标准层表校验语句管理", remark = "修改标准层表校验语句信息")
    @RequestMapping(value = "/edit")
    @ResponseBody
    public RestResult edit(TableCheck tableCheck) throws Exception {
        if (tableCheck == null || StringUtils.isBlank(tableCheck.getId())
                || StringUtils.isBlank(tableCheck.getTableId())) {
            throw new BusinessException("获取标准层表校验语句信息失败，请稍后重试！");
        }
        tableCheck.setEditUser(redisCacheManager.getLoginUserInfo().getLoginId());
        tableCheck.setEditTime(LocalDateTime.now());
        tableCheckService.updateById(tableCheck);
        return ResultGenerator.getSuccessResult("修改标准层表校验语句信息成功！");
    }

    /**
     * 删除标准层表校验语句信息
     *
     * @return
     */
    @YssPermissions(value = {"reportworld:table:check:del"})
    @ApiOperation(value = "删除标准层表校验语句信息")
    @YssLog(module = "标准层表校验语句管理", remark = "删除标准层表校验语句信息")
    @RequestMapping(value = "/del")
    @ResponseBody
    public RestResult del(String ids) throws Exception {
        if (StringUtils.isBlank(ids)) {
            throw new BusinessException("获取删除标准层表校验语句信息失败，请稍后重试！");
        }
        List<String> listId = JSONArray.parseArray(ids, String.class);
        tableCheckService.removeByIds(listId);
        return ResultGenerator.getSuccessResult("删除标准层表校验语句信息成功！");
    }
}
